package com.peach.controller.UserManage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.peach.domain.ResultVO;
import com.peach.domain.UserManage.Manager;
import com.peach.domain.UserManage.User;
import com.peach.service.UserManage.ManagerApplication;
import com.peach.utils.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    private ManagerApplication managerApplication;


    @RequestMapping("/login")
    @ResponseBody
    public ResultVO managerLogin (HttpServletRequest req , HttpServletResponse res){
        HttpSession session = req.getSession();
        String reqJson = null;
        try {
            reqJson = JsonConfig.getRequestPostStr(req);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject json = JSON.parseObject(reqJson);
        String id = json.getString("userName");
        String passWord = json.getString("passWord");

        Manager manager= new Manager();
        manager.setId(id);
        manager.setPassWord(passWord);

        ResultVO rvo = new ResultVO();
        List<Manager> managers = managerApplication.login(manager);


        if (managers.size() > 0){
            rvo.setSuccess(true);
            Map<String,String> managerMap = new HashMap<String,String>();
            Manager managerVO = new Manager();
            managerMap.put("id",id);
            managerMap.put("managerName",managers.get(0).getManagerName());
            managerMap.put("passWord",passWord);
            managerMap.put("admin",managers.get(0).getAdmin());
            managerVO.setManager(managerMap);
            rvo.setData(managerVO);
            session.setAttribute("isLogin",true);
            session.setAttribute("currentUser",id);
        }else {
            rvo.setSuccess(false);
            rvo.setMsg("账号或密码错误");
            session.setAttribute("isLogin",false);
        }
        return rvo;
    }

    @RequestMapping("/getAllUser")
    @ResponseBody
    public ResultVO getAllUser(HttpServletRequest req,HttpServletResponse res){
        HttpSession session = req.getSession();
        String reqJson = null;
        try {
            reqJson = JsonConfig.getRequestPostStr(req);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject json = JSON.parseObject(reqJson);
        List<User> users = managerApplication.getAllUser();
        ResultVO rvo = new ResultVO();
        rvo.setSuccess(true);
        rvo.setData(users);
        return rvo;
    }
    //TODO：冻结用户账户接口（需要在用户表中增加一个"可用"字段，并且在用户登录的逻辑中添加判断控制。）
    //TODO：查看所有用户，用户管理下的所有设备功能。
    //消息管理
    //TODO：查询所有消息发送记录接口
    //TODO：查询所有指令发送记录接口
    //TODO：发送消息接口
}
